package www.wen.com.common.lib.view.popuview

import android.content.Context
import android.view.View
import android.widget.TextView
import com.lxj.xpopup.core.CenterPopupView
import com.lxj.xpopup.util.XPopupUtils
import www.wen.com.common.R
import www.wen.com.common.lib.extension.setOnClickListenerAgency
import www.wen.com.common.lib.helper.CancelFunction
import www.wen.com.common.lib.helper.ConfirmFunction

/**
 * @author WEN
 * @Description:这是一个基础只有内容、确认、取消的警告弹窗
 * @date 2021/3/3 14:53
 */
abstract class BaseAlertPopupView(
    context: Context,
    open val title: CharSequence,
    open val content: CharSequence,
    open val cancelContent: CharSequence,
    open val confirmContent: CharSequence,
    open val layoutId: Int,
    private val finishOnCancel: Boolean = true,
    open var cancelFunction: CancelFunction? = null,
    open var confirmFunction: ConfirmFunction? = null
) : CenterPopupView(context), View.OnClickListener {

    private lateinit var cancelView: TextView

    private lateinit var enterView: TextView

    private lateinit var tvContent: TextView

    private lateinit var tvTitle: TextView

    override fun getImplLayoutId(): Int = layoutId

    override fun onClick(v: View) {

        when (v.id) {
            R.id.cancel -> {
                if (finishOnCancel) {
                    dismiss()
                }
                cancelFunction?.run {
                    invoke(this@BaseAlertPopupView)
                }
            }
            R.id.confirm -> {
                confirmFunction?.run {
                    invoke(this@BaseAlertPopupView)
                }
                if (popupInfo.autoDismiss) dismiss()
            }
        }
    }

    override fun initPopupContent() {
        super.initPopupContent()

        cancelView = findViewById(R.id.cancel)
        enterView = findViewById(R.id.confirm)

        tvContent = findViewById(R.id.tvContent)
        tvTitle = findViewById(R.id.tvTitle)

        tvContent.text = content
        tvTitle.text = title
        cancelView.text = cancelContent
        enterView.text = confirmContent

        setOnClickListenerAgency(this, cancelView, enterView)
    }

    override fun getMaxWidth(): Int {
        return (XPopupUtils.getScreenWidth(context) * 0.8f).toInt()
    }

}


